use crate::xueliuhz::{
    fx_258, fx_bai_wan_dan, fx_bian, fx_da_yu_wu, fx_dai_yao_jiu, fx_dan_diao, fx_dan_diao_hz,
    fx_duan_yao_jiu, fx_jiang_dui, fx_jin_gou_diao, fx_jiu_lian_bao_deng, fx_jue_zhang, fx_kan,
    fx_lao_shao_pei, fx_lian_liu, fx_lian_qi_dui, fx_lv_yi_se, fx_men_qing, fx_peng_peng_hu,
    fx_qi_dui, fx_qing_yi_se, fx_quan_dai_wu, fx_quan_shuang_ke, fx_quan_yao_jiu, fx_san_an_ke,
    fx_san_jie_gao, fx_shi_er_jin_chai, fx_shuang_an_ke, fx_shuang_tong_ke, fx_si_an_ke,
    fx_si_jie_gao, fx_xiao_yu_wu, fx_yi_ban_gao, fx_yi_se_shuang_long_hui, fx_yi_tiao_long,
    fx_zhuo_wu_kui, XlhzTiles, MJRet,
};
use lazy_static::lazy_static;
use log::info;
use mahjong_lib::*;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use MJFanXing::*;

#[repr(u16)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum MJFanXing {
    HUJIAOZHUANYI,

    // 翻倍(可叠加)
    GEN,
    GANGSHANGKAIHUA,
    GANGSHANGPAO,
    QIANGGANGHU,
    HAIDILAOYUE,
    MIAOSHOUHUICHUN,
    BIANZHANG,
    JUEZHANG,
    KANZHANG,
    MENQING,

    //基本翻型(不叠加)
    PINGHU,
    QINGYISE,
    TIANHU,
    DIHU,

    SHUANGANKE,
    LAOSHAOPEI,
    BUQIUREN,
    YITIAOLONG,
    SANANKE,
    DAYUWU,
    XIAOYUWU,
    QUANSHUANGKE,
    SANJIEGAO,
    SHIERJINCHAI,
    SIANKE,
    SIJIEGAO,
    QUANDAIWU,
    LVYISE,
    JIULIANBAODENG,
    YISESHUANGLONGHUI,
    LIANQIDUI,

    PENGPENGHU,
    JINGOUDIAO,
    SHIBALUOHAN,

    QIDUI,
    LONGQIDUI,
    SHUANGLONGQIDUI,
    SANLONGQIDUI,

    DAIYAOJIU,
    QUANYAOJIU,
    DUANYAOJIU,
    JIANGDUI,

    SHOUZHONGBAOYI,
    BAIWANDAN,
    ZHUOWUKUI,
    DANDIAO,
    YIBANGAO,
    LIULIANSHUN,
    SHUANGTONGKE,
    SUHU,
}

pub const ORDER_FAN: [MJFanXing; 41] = [
    SANLONGQIDUI,
    SHIBALUOHAN,
    YISESHUANGLONGHUI,
    QUANYAOJIU,
    LIANQIDUI,
    SHUANGLONGQIDUI,
    JIULIANBAODENG,
    SIANKE,
    QUANDAIWU,
    LONGQIDUI,
    SHOUZHONGBAOYI,
    LVYISE,
    SIJIEGAO,
    DAIYAOJIU,
    SHIERJINCHAI,
    JIANGDUI,
    QIDUI,
    SANANKE,
    BAIWANDAN,
    QUANSHUANGKE,
    SANJIEGAO,
    JINGOUDIAO,
    XIAOYUWU,
    DAYUWU,
    YITIAOLONG,
    QINGYISE,
    BUQIUREN,
    SHUANGANKE,
    ZHUOWUKUI,
    SHUANGTONGKE,
    SUHU,
    LAOSHAOPEI,
    PENGPENGHU,
    DUANYAOJIU,
    DANDIAO,
    YIBANGAO,
    LIULIANSHUN,
    MENQING,
    BIANZHANG,
    KANZHANG,
    JUEZHANG,
];

pub type FxFunc = fn(&XlhzTiles) -> u64;
pub type RmFunc = fn(&mut XlhzTiles, r: u64) -> u64;

#[derive(Clone, Copy)]
pub struct MJFanXingDetail {
    pub eng_name: &'static str,
    pub cn_name: &'static str,
    pub value: i32,
}

lazy_static! {

    pub static ref FAN_PATTERN: HashMap<MJFanXing, MJFanXingDetail> = {
        let mut m = HashMap::new();
        m.insert(MJFanXing::SANLONGQIDUI, MJFanXingDetail { eng_name: "sanlongqidui", cn_name: "三龙七对", value: 288 });
        m.insert(MJFanXing::SHIBALUOHAN, MJFanXingDetail { eng_name: "shibaluohan", cn_name: "十八罗汉", value: 128 });
        m.insert(MJFanXing::YISESHUANGLONGHUI, MJFanXingDetail { eng_name: "yiseshuanglonghui", cn_name: "一色双龙会", value: 128 });
        m.insert(MJFanXing::QUANYAOJIU, MJFanXingDetail { eng_name: "quanyaojiu", cn_name: "全幺九", value: 96 });
        m.insert(MJFanXing::LIANQIDUI, MJFanXingDetail { eng_name: "lianqidui", cn_name: "连七对", value: 96 });
        // ... 继续补全其他番型
        // 注意，这里只是示例，实际上您需要根据C++代码中的完整列表来补全
        m
    };

    pub static ref FAN_FUNC: HashMap<MJFanXing, FxFunc> = {
        let mut ffm: HashMap<MJFanXing, FxFunc> = HashMap::from([
            (DAYUWU, fx_da_yu_wu as FxFunc),
            (JUEZHANG, fx_jue_zhang as FxFunc),
            (BIANZHANG, fx_bian as FxFunc),
            (KANZHANG, fx_kan as FxFunc),
            (QINGYISE, fx_qing_yi_se as FxFunc),
            (PENGPENGHU, fx_peng_peng_hu as FxFunc),
            (JINGOUDIAO, fx_jin_gou_diao as FxFunc),
//            (SHIBALUOHAN, fx_shi_ba_luo_han as FxFunc),
            (QIDUI, fx_qi_dui as FxFunc),
            (QUANYAOJIU, fx_quan_yao_jiu as FxFunc),
            (DAIYAOJIU, fx_dai_yao_jiu as FxFunc),
            (DUANYAOJIU, fx_duan_yao_jiu as FxFunc),
            (JIANGDUI, fx_jiang_dui as FxFunc),
            (LAOSHAOPEI, fx_lao_shao_pei as FxFunc),
            (YITIAOLONG, fx_yi_tiao_long as FxFunc),
            (DAYUWU, fx_da_yu_wu as FxFunc),
            (XIAOYUWU, fx_xiao_yu_wu as FxFunc),
            (QUANSHUANGKE, fx_quan_shuang_ke as FxFunc),
            (SANJIEGAO, fx_san_jie_gao as FxFunc),
            (SHIERJINCHAI, fx_shi_er_jin_chai as FxFunc),
            (SIJIEGAO, fx_si_jie_gao as FxFunc),
            (QUANDAIWU, fx_quan_dai_wu as FxFunc),
            (LVYISE, fx_lv_yi_se as FxFunc),
            (JIULIANBAODENG, fx_jiu_lian_bao_deng as FxFunc),
            (YISESHUANGLONGHUI, fx_yi_se_shuang_long_hui as FxFunc),
            (LIANQIDUI, fx_lian_qi_dui as FxFunc),
            (ZHUOWUKUI, fx_zhuo_wu_kui as FxFunc),
            (DANDIAO, fx_dan_diao as FxFunc),
            (BAIWANDAN, fx_bai_wan_dan as FxFunc),
            (LIULIANSHUN, fx_lian_liu as FxFunc),
            (YIBANGAO, fx_yi_ban_gao as FxFunc),
            (SHOUZHONGBAOYI, fx_dan_diao_hz as FxFunc),
            (SIANKE, fx_si_an_ke as FxFunc),
            (SANANKE, fx_san_an_ke as FxFunc),
            (SHUANGANKE, fx_shuang_an_ke as FxFunc),
            (SHUANGTONGKE, fx_shuang_tong_ke as FxFunc),
        ]);
        ffm
    };
    pub static ref FAN_RMFUNC: HashMap<MJFanXing, RmFunc> = {
        let mut ffm: HashMap<MJFanXing, RmFunc> = HashMap::from([]);
        ffm
    };
    pub static ref FAN_MATRIX: HashMap<MJFanXing, Vec<MJFanXing>> = {
        let mut fm: HashMap<MJFanXing, Vec<MJFanXing>> = HashMap::from([
            (
                SANLONGQIDUI,
                vec![
                    QUANYAOJIU, LVYISE, DAIYAOJIU, BAIWANDAN, DAYUWU, XIAOYUWU, QINGYISE, SUHU,
                    DUANYAOJIU, JUEZHANG,
                ],
            ),
            (
                SHIBALUOHAN,
                vec![
                    QUANYAOJIU,
                    SIANKE,
                    LVYISE,
                    SIJIEGAO,
                    DAIYAOJIU,
                    JIANGDUI,
                    SANANKE,
                    BAIWANDAN,
                    QUANSHUANGKE,
                    SANJIEGAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    DUANYAOJIU,
                    MENQING,
                ],
            ),
            (
                YISESHUANGLONGHUI,
                vec![SUHU, DANDIAO, BIANZHANG, KANZHANG, JUEZHANG],
            ),
            (
                QUANYAOJIU,
                vec![
                    SHUANGLONGQIDUI,
                    SIANKE,
                    LONGQIDUI,
                    SHIERJINCHAI,
                    SANANKE,
                    BUQIUREN,
                    SHUANGANKE,
                    SUHU,
                    PENGPENGHU,
                    DANDIAO,
                    MENQING,
                ],
            ),
            (LIANQIDUI, vec![SUHU, DUANYAOJIU]),
            (
                SHUANGLONGQIDUI,
                vec![
                    LVYISE, BAIWANDAN, DAYUWU, XIAOYUWU, QINGYISE, SUHU, DUANYAOJIU,
                ],
            ),
            (
                JIULIANBAODENG,
                vec![
                    YITIAOLONG, SHUANGANKE, SUHU, DANDIAO, BIANZHANG, KANZHANG, JUEZHANG,
                ],
            ),
            (
                SIANKE,
                vec![
                    LVYISE,
                    SIJIEGAO,
                    DAIYAOJIU,
                    JIANGDUI,
                    SHIERJINCHAI,
                    BAIWANDAN,
                    QUANSHUANGKE,
                    SANJIEGAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    SHUANGTONGKE,
                    SUHU,
                    DUANYAOJIU,
                    DANDIAO,
                ],
            ),
            (
                QUANDAIWU,
                vec![
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    DANDIAO,
                    YIBANGAO,
                    MENQING,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (LONGQIDUI, vec![QINGYISE, SUHU, DUANYAOJIU]),
            (
                SHOUZHONGBAOYI,
                vec![
                    SIJIEGAO,
                    SHIERJINCHAI,
                    JIANGDUI,
                    SANANKE,
                    BAIWANDAN,
                    QUANSHUANGKE,
                    SANJIEGAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    MENQING,
                ],
            ),
            (
                LVYISE,
                vec![
                    SHIERJINCHAI,
                    SANANKE,
                    QUANSHUANGKE,
                    SANJIEGAO,
                    JINGOUDIAO,
                    XIAOYUWU,
                    BUQIUREN,
                    SHUANGANKE,
                    SUHU,
                    PENGPENGHU,
                    KANZHANG,
                    JUEZHANG,
                    MENQING,
                ],
            ),
            (
                SIJIEGAO,
                vec![
                    SHIERJINCHAI,
                    SANANKE,
                    BAIWANDAN,
                    JINGOUDIAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    DUANYAOJIU,
                    DANDIAO,
                    JUEZHANG,
                    MENQING,
                ],
            ),
            (
                DAIYAOJIU,
                vec![
                    SHIERJINCHAI,
                    SANANKE,
                    XIAOYUWU,
                    DAYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    LAOSHAOPEI,
                    PENGPENGHU,
                    DANDIAO,
                    YIBANGAO,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                SHIERJINCHAI,
                vec![
                    JIANGDUI,
                    SANANKE,
                    BAIWANDAN,
                    QUANSHUANGKE,
                    SANJIEGAO,
                    JINGOUDIAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                JIANGDUI,
                vec![
                    SANANKE,
                    QUANSHUANGKE,
                    JINGOUDIAO,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    DANDIAO,
                    MENQING,
                ],
            ),
            (
                SANANKE,
                vec![
                    BAIWANDAN,
                    QUANSHUANGKE,
                    SANJIEGAO,
                    JINGOUDIAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (QIDUI, vec![QINGYISE, SUHU, DUANYAOJIU]),
            (
                BAIWANDAN,
                vec![
                    SANJIEGAO,
                    JINGOUDIAO,
                    DAYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                QUANSHUANGKE,
                vec![
                    JINGOUDIAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    DANDIAO,
                    MENQING,
                ],
            ),
            (
                SANJIEGAO,
                vec![
                    JINGOUDIAO,
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                JINGOUDIAO,
                vec![
                    DAYUWU,
                    XIAOYUWU,
                    QINGYISE,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    DUANYAOJIU,
                ],
            ),
            (
                XIAOYUWU,
                vec![
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                DAYUWU,
                vec![
                    QINGYISE,
                    BUQIUREN,
                    SHUANGANKE,
                    SHUANGTONGKE,
                    SUHU,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                YITIAOLONG,
                vec![
                    QINGYISE, BUQIUREN, ZHUOWUKUI, SUHU, DANDIAO, MENQING, BIANZHANG, KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                QINGYISE,
                vec![
                    BUQIUREN,
                    SHUANGANKE,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    LAOSHAOPEI,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                BUQIUREN,
                vec![
                    SHUANGANKE,
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    LAOSHAOPEI,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                SHUANGANKE,
                vec![
                    ZHUOWUKUI,
                    SHUANGTONGKE,
                    SUHU,
                    LAOSHAOPEI,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                ZHUOWUKUI,
                vec![
                    SHUANGTONGKE,
                    SUHU,
                    LAOSHAOPEI,
                    DUANYAOJIU,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    JUEZHANG,
                ],
            ),
            (
                SHUANGTONGKE,
                vec![
                    SUHU,
                    LAOSHAOPEI,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                SUHU,
                vec![
                    LAOSHAOPEI,
                    PENGPENGHU,
                    DUANYAOJIU,
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                LAOSHAOPEI,
                vec![
                    DANDIAO,
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (PENGPENGHU, vec![DUANYAOJIU, DANDIAO, MENQING]),
            (
                DUANYAOJIU,
                vec![DANDIAO, YIBANGAO, LIULIANSHUN, MENQING, KANZHANG, JUEZHANG],
            ),
            (
                DANDIAO,
                vec![
                    YIBANGAO,
                    LIULIANSHUN,
                    MENQING,
                    BIANZHANG,
                    KANZHANG,
                    JUEZHANG,
                ],
            ),
            (
                YIBANGAO,
                vec![LIULIANSHUN, MENQING, BIANZHANG, KANZHANG, JUEZHANG],
            ),
            (LIULIANSHUN, vec![MENQING, BIANZHANG, KANZHANG, JUEZHANG]),
            (MENQING, vec![BIANZHANG, KANZHANG, JUEZHANG]),
            (BIANZHANG, vec![JUEZHANG]),
            (KANZHANG, vec![JUEZHANG]),
            (JUEZHANG, vec![]),
        ]);
        fm
    };
}
